<template>
  <el-row class="login-page">
    <el-col :span="12">
      <div class="bg"></div>
    </el-col>
    <el-col :span="6" :offset="3" class="form" v-if="!register">
      <el-form :rules="rules" :model="loginUser" label-width="auto" style="max-width: 600px">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="loginUser.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="loginUser.password" type="password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item>
          <div class="btn">
            <el-checkbox value="1" name="type">记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-btn" @click="loginToken">登录</el-button>
        </el-form-item>
      </el-form>
      <el-form-item>
        <el-link type="info" underline="never" @click="register = true">注册→</el-link>
      </el-form-item>
    </el-col>
    <el-col :span="6" :offset="3" class="form" v-else>
      <el-form :rules="rules" :model="rememberUser" label-width="auto" style="max-width: 600px" ref="form" class="form">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="rememberUser.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="rememberUser.password" type="password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item prop="repassword">
          <el-input :prefix-icon="Lock" v-model="rememberUser.repassword" type="password" placeholder="请再次输入密码" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-btn" @click="registerUser">注册</el-button>
        </el-form-item>
      </el-form>
      <el-form-item>
        <el-link type="info" underline="never" @click="register = false">← 返回</el-link>
      </el-form-item>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { reg, login } from '@/api/user'
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loginUser = ref({
  username: '',
  password: '',
  remember: 0,
  rePassword: ''
})
const rememberUser = ref({
  username: '',
  password: '',
  repassword: ''
})
const register = ref(false)

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
        if (value !== rememberUser.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback(undefined)
        }
      },
      trigger: 'blur'
    }
  ]
}

const form = ref()
const registerUser = async () => {
  await form.value.validate()
  await reg(rememberUser.value)
  ElMessage.success("注册成功！")
  rememberUser.value = {
    username: '',
    password: '',
    repassword: ''
  }
}

const loginToken = async () => {
  await login(loginUser.value)
  await router.push("/")
  ElMessage.success("登录成功！")
  loginUser.value = {
    username: '',
    password: '',
    remember: 0,
    rePassword: ''
  }
}
</script>

<style scoped>
.login-page {
  height: 100vh;
  background-color: #fff;

  .bg {
    height: 100%;
    background: url('../../assets/logo2.png') no-repeat 60% center / 240px auto,
      url('../../assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }

  .btn {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .login-btn {
      width: 100%;
    }
  }
}
</style>
